<?php 
namespace Client\Controller;

use Think\Image;

use Think\Controller;

class RegisterController extends Controller{
	public function getVerify(){
		//file_put_contents("Public/Log/log.txt", "post:".json_encode($_POST)."\r\n",FILE_APPEND|LOCK_EX);
		if(IS_POST){
			//file_put_contents("Public/Log/log.txt", "post:".json_encode($_POST)."\r\n",FILE_APPEND|LOCK_EX);
			$phone = trim($_POST['phone']);
			if(preg_match("/^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$/",$phone)){    
   		    	//验证通过,先通过短信接口发给用户短信，然后再传验证码给app
   		    	if(M('account')->where(array('phone'=>$phone))->find()){    
   		    		$this->ajaxReturn(array('result'=>'-3','msg'=>'该手机号码已经注册过了'));
   		    	}
        			$this->ajaxReturn(array('result'=>'0','msg'=>'手机号码通过验证'));
			}else{    
    			//手机号码格式不对    
        		$this->ajaxReturn(array('result'=>'-1','msg'=>'手机号码格式不正确'));
			} 
		}
		$this->ajaxReturn(array('result'=>'-2','msg'=>'数据不是以POST方式提交'));
	}
	
	public function finish(){
		 if(IS_POST){
		 	$phone = $_POST['phone'];
		 	file_put_contents("Public/Log/log.txt", "post:".json_encode($_POST)."\r\n",FILE_APPEND|LOCK_EX);
				$account = D('Account');
				$data['phone'] = $phone;
				$data['password'] = $_POST['password'];
				if($account->create($data)){
					$account->add();
					$userInfo = $account->where(array('phone'=>$phone))->find();
					$json['result']="0";
					$json['msg']="成功";
					
					vendor("rongcloud.ServerAPI");
					$api = new \ServerAPI();
					$back =  json_decode($api->getToken($userInfo['id']),'json');
					$userInfo['token'] = $back['token'];
					
					$json['userInfo']=$userInfo;
					$this->ajaxReturn($json);
				}
				else{
					$this->ajaxReturn(array('result'=>'-1','msg'=>$account->getError()));
				}	
			}
			else{
				$this->ajaxReturn(array('result'=>'-3','msg'=>'数据不是以POST提交'));
				S($phone,null);
			}
		}
 } 
?>